package com.penghe.www.httpprocessor

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import android.widget.TextView
import com.penghe.www.httplibrary.HttpCallBack
import com.penghe.www.httplibrary.HttpHelper

/**
 * Created by pengh on 2017/5/29.
 * 主界面
 */

class MainActivity : AppCompatActivity() {

    private var resultTv: TextView? = null
    private var requestBtn: Button? = null
    private val TAG: String = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Example of a call to a native method
        resultTv = findViewById(R.id.text) as TextView
        resultTv!!.text = stringFromJNI()
        requestBtn = findViewById(R.id.button) as Button

        requestBtn!!.setOnClickListener {
            view ->
            val httpHelper: HttpHelper = HttpHelper.obtain()

            httpHelper.get("http://www.fk68.net/temp/dn.json", hashMapOf(),object : HttpCallBack<JsonBeen>() {
                override fun onFailed(message: String) {
                    Log.w(TAG,"onFailed " + message)
                }

                override fun onSucceeded(result: JsonBeen) {
                    val stringBuffer:StringBuffer = StringBuffer()
                    stringBuffer.append("namne = ").append(result.getName()).append(" school name = ").append(result.getShoolname())
                    resultTv!!.text = stringBuffer.toString()
                }
            })
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {

        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }
}
